package com.ws.shop.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.ws.shop.dao.AdminRoleDao;
import com.ws.shop.entity.AdminRole;

@Repository(value="adminRoleDao")
public class AdminRoleDaoImpl extends BaseDaoImpl implements AdminRoleDao {

	public Object[] deleteRoleById(int id) {
		this.update("delete from admin_role where roleid=?", id);
		return new Object[]{0,"删除成功"};
	}

	public AdminRole findRoleById(final int id) {
		return this.query("select r1.name,r1.descs,r1.status,r1.proleid,r1.creattime,r2.name from admin_role r1 left join admin_role r2 on r1.proleid=r2.roleid where r1.roleid=?",
				new  ResultSetExtractor<AdminRole>(){
					public AdminRole extractData(ResultSet rs) throws SQLException,
						DataAccessException {
						if(!rs.next())
							return null;
						AdminRole role = new AdminRole();
						role.setRoleid(id);
						role.setName(rs.getString(1));
						role.setDescs(rs.getString(2));
						role.setStatus(rs.getInt(3));
						role.setProleid(rs.getInt(4));
						role.setCreattime(rs.getTimestamp(5));
						role.setPname(rs.getString(6));
						return role;
					}
				}, id);
	}

	public List<AdminRole> findRoleListAll() {
		return this.query("select r1.roleid,r1.name,r1.descs,r1.status,r1.proleid,r1.creattime,r2.name from admin_role r1 left join admin_role r2 on r1.proleid=r2.roleid order by r1.roleid", new ResultSetExtractor<List<AdminRole>>(){
			public List<AdminRole> extractData(ResultSet rs)
					throws SQLException, DataAccessException {
				List<AdminRole> list = new LinkedList<AdminRole>();
				AdminRole role = null;
				while(rs.next()){
					role = new AdminRole();
					role.setRoleid(rs.getInt(1));
					role.setName(rs.getString(2));
					role.setDescs(rs.getString(3));
					role.setStatus(rs.getInt(4));
					role.setProleid(rs.getInt(5));
					role.setCreattime(rs.getTimestamp(6));
					role.setPname(rs.getString(7));
					list.add(role);
				}
				return list;
			}
		});
	}

	public void saveOrUpdate(AdminRole role) {
		if(role.getRoleid() == 0){
			this.update("insert into admin_role(name,descs,status,proleid,creattime) values (?,?,?,?,now())",
						role.getName(),role.getDescs(),role.getStatus(),role.getProleid());
		}else{
			this.update("update admin_role set name=?,descs=?,status=?,proleid=? where roleid=?",
						role.getName(),role.getDescs(),role.getStatus(),role.getProleid(),role.getRoleid());
		}
	}

	final String deleteRoleMenuByRoleid = "delete from admin_role_menu where roleid=?";
	final String saveRoleMemus = "insert into admin_role_menu(roleid,menuid) values(?,?)";
	public void saveAdminRolePerm(final int roleid,final int[] menuids) {
		this.update(deleteRoleMenuByRoleid, roleid);
		this.batchUpdate(saveRoleMemus, new BatchPreparedStatementSetter(){
			public int getBatchSize() {
				return menuids.length;
			}

			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
				ps.setInt(1, roleid);
				ps.setInt(2, menuids[i]);
			}
		});
	}

}
